import { useState, useCallback } from 'react';
import { apiClient } from '@/api/client';
import {
  Activity,
  ActivityType,
  MoodConfig,
  User,
  UserSettings,
  CreateActivityRequest,
  UpdateActivityRequest,
  GetActivitiesRequest,
  GetStatsRequest,
  CreateActivityTypeRequest,
  UpdateActivityTypeRequest,
  CreateMoodConfigRequest,
  UpdateMoodConfigRequest,
  UpdateUserSettingsRequest,
} from '@/types/proto';

export function useActivities() {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const createActivity = useCallback(async (request: CreateActivityRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.createActivity(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to create activity');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const getActivities = useCallback(async (request: GetActivitiesRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.getActivities(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to get activities');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const updateActivity = useCallback(async (request: UpdateActivityRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.updateActivity(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to update activity');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const deleteActivity = useCallback(async (id: string) => {
    try {
      setLoading(true);
      setError(null);
      await apiClient.deleteActivity(id);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to delete activity');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    createActivity,
    getActivities,
    updateActivity,
    deleteActivity,
    loading,
    error,
  };
}

export function useSettings() {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const getActivityTypes = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.getActivityTypes();
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to get activity types');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const createActivityType = useCallback(async (request: CreateActivityTypeRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.createActivityType(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to create activity type');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const getMoodConfigs = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.getMoodConfigs();
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to get mood configs');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const getUserSettings = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.getUserSettings();
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to get user settings');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const updateUserSettings = useCallback(async (request: UpdateUserSettingsRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.updateUserSettings(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to update user settings');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const updateActivityType = useCallback(async (request: UpdateActivityTypeRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.updateActivityType(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to update activity type');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const deleteActivityType = useCallback(async (id: string) => {
    try {
      setLoading(true);
      setError(null);
      await apiClient.deleteActivityType(id);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to delete activity type');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const createMoodConfig = useCallback(async (request: CreateMoodConfigRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.createMoodConfig(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to create mood config');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const updateMoodConfig = useCallback(async (request: UpdateMoodConfigRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.updateMoodConfig(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to update mood config');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const deleteMoodConfig = useCallback(async (id: string) => {
    try {
      setLoading(true);
      setError(null);
      await apiClient.deleteMoodConfig(id);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to delete mood config');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    getActivityTypes,
    createActivityType,
    updateActivityType,
    deleteActivityType,
    getMoodConfigs,
    createMoodConfig,
    updateMoodConfig,
    deleteMoodConfig,
    getUserSettings,
    updateUserSettings,
    loading,
    error,
  };
}

export function useUser() {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const getUser = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.getUser();
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to get user');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const getStats = useCallback(async (request: GetStatsRequest) => {
    try {
      setLoading(true);
      setError(null);
      const result = await apiClient.getStats(request);
      return result;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to get stats');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    getUser,
    getStats,
    loading,
    error,
  };
}